<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\dao\car;


use app\dao\BaseDao;
use app\model\car\CarOrderRerent;

/**
 * Class CarOrderRerentDao
 * @package app\dao\car
 */
class CarOrderRerentDao extends BaseDao
{
    protected function setModel(): string
    {
        return CarOrderRerent::class;
    }

    /**
     * 列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public function getList(array $where = [], int $page, int $limit){
        $model = $this->getModel()->alias('r')->where('r.is_del',0);
        $model->join('car_order o','o.id=r.order_id','left');
        $model->join('car_staff s','s.id=r.staff_id','left');
        $model->join('base_place p','p.id=s.place_id','left');
        $model->field('r.*,o.order_no,s.user_name,s.user_phone,p.title as place_name,o.contact_name');
        $model = $this->setWheres($model,$where);
        $count = $model->count();
        $list = $model->page($page,$limit)->order('add_time desc')->select()->toArray();
        return compact('list','count');
    }

    /**
     * 查询条件
     * @param $model
     * @param $where
     * @return mixed
     */
    public function setWheres($model,$where){
        // 关键字
        if(isset($where['keyword']) && $where['keyword']){
            if($where['field_key'] == 'all'){
                $model->where('o.order_no|o.contact_name|r.uid|r.rerent_no','like',"%{$where['keyword']}%");
            }elseif($where['field_key'] == 'uid'){
                $model->where('r.uid',$where['keyword']);
            }elseif($where['field_key'] == 'order_no'){
                $model->where('o.order_no','like',"%{$where['keyword']}%");
            }elseif($where['field_key'] == 'rerent_no'){
                $model->where('r.rerent_no','like',"%{$where['keyword']}%");
            }
        }
        // 状态
        if($where['rerent_status'] !== ''){
            $model->where('r.rerent_status','=',$where['rerent_status']);
        }
        // UID
        if(isset($where['uid']) && $where['uid']){
            $model->where('r.uid','=',$where['uid']);
        }
        // 支付方式
        if(isset($where['pay_type']) && $where['pay_type']){
            $model->where('r.pay_type','=',$where['pay_type']);
        }
        // ids
        if(isset($where['ids']) && $where['ids']){
            $model->where('r.id','in',$where['ids']);
        }
        // 订单
        if(isset($where['order_id']) && $where['order_id']){
            $model->where('r.order_id','=',$where['order_id']);
        }
        // 门店
        if(isset($where['store_id']) && $where['store_id']){
            $model->where('o.store_id', $where['store_id']);
        }
        return $model;
    }
    /**
     * 导出续租订单
     * @param array $where
     * @return mixed
     */
    public function exportOrderRerent(array $where = []){
        $model = $this->getModel()->alias('r')->where('r.is_del',0);
        $model->join('car_order o','o.id=r.order_id','left');
        $model->field('r.rerent_no,o.order_no,o.contact_name,o.type_name,r.rerent_price,r.start_time,r.end_time,r.pay_time,r.add_time,r.rerent_status');
        $model = $this->setWheres($model,$where);
        $list = $model->select()->toArray();
        return $list;
    }
}
